//
//  HWPromoteTopView.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/27.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit
import FSPagerView

class HWPromoteTopView: UIView {
    var searchBgView:UIView!
    var searchLogoImage:UIImageView!
    var searchLabel:UILabel!
    var searchButton:UIButton!
    var bgView:UIView!
    /// 推广公告图标
    var promoteNoticeImageView:UIImageView!
    /// 推广公告
    var promoteNoticeLabel:UILabel!
    var promoteNoticeButton:UIButton!
    /// 推广二维码
    var promoteQRCodeImageView:UIImageView!
    /// 推广二维码
    var promoteQRCodeLabel:UILabel!
    var promoteQRCodeButton:UIButton!
    /// 分成明细图标
    var divideDetailImageView:UIImageView!
    /// 分成明细
    var divideDetailLabel:UILabel!
    var divideDetailButton:UIButton!
    /// 人气榜图标
    var popularityImageView:UIImageView!
    /// 人气榜
    var popularityLabel:UILabel!
    var popularityButton:UIButton!
    
    var bottomView:UIView!
    var noticeImageView:UIImageView!
    var titleBanner:FSPagerView!
    
    var searchButtonClick:(()->())?
    var promoteNoticeButtonClick:(()->())?
    var promoteQRCodeButtonClick:(()->())?
    var divideDetailButtonClick:(()->())?
    var popularityButtonClick:(()->())?
    var didClickBanner:((_:HWNoticeModel)->())?
    var models:[HWNoticeModel]? {
        didSet {
            self.titleBanner.reloadData()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension HWPromoteTopView {
    private func initUI() -> Void {
        backgroundColor = .colorWithHexString(hexString: "#F5F5F5")
        
        searchBgView = UIView()
        searchBgView.backgroundColor = .white
        searchBgView.layer.cornerRadius = 12*widthScale
        searchBgView.layer.borderWidth = 1*widthScale
        searchBgView.layer.borderColor = UIColor.colorWithHexString(hexString: "#CCCCCC").cgColor
        addSubview(searchBgView)
        
        searchLogoImage = UIImageView()
        searchLogoImage.image = UIImage(named: "search")
        searchBgView.addSubview(searchLogoImage)
        
        searchLabel = HWLabel.createLabel(title: "搜索游戏", color: "#AAAAAA", font: 14*widthScale)
        searchBgView.addSubview(searchLabel)
        
        searchButton = UIButton(type: .custom)
        searchButton.addTarget(self, action: #selector(searchButtonClick(_:)), for: .touchUpInside)
        searchBgView.addSubview(searchButton)
        
        bgView = UIView()
        bgView.backgroundColor = .white
        bgView.layer.cornerRadius = 12*widthScale
        bgView.layer.masksToBounds = true
        addSubview(bgView)
        
        promoteNoticeImageView = UIImageView()
        promoteNoticeImageView.image = UIImage(named: "promoteNotice")
        bgView.addSubview(promoteNoticeImageView)
        
        promoteNoticeLabel = HWLabel.createBoldLabel(title: "推广公告", color: "#333333", font: 14*widthScale)
        bgView.addSubview(promoteNoticeLabel)
        
        promoteNoticeButton = UIButton(type: .custom)
        promoteNoticeButton.addTarget(self, action: #selector(promoteNoticeButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(promoteNoticeButton)
        
        promoteQRCodeImageView = UIImageView()
        promoteQRCodeImageView.image = UIImage(named: "promoteQRCode")
        bgView.addSubview(promoteQRCodeImageView)
        
        promoteQRCodeLabel = HWLabel.createBoldLabel(title: "推广二维码", color: "#333333", font: 14*widthScale)
        bgView.addSubview(promoteQRCodeLabel)
        
        promoteQRCodeButton = UIButton(type: .custom)
        promoteQRCodeButton.addTarget(self, action: #selector(promoteQRCodeButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(promoteQRCodeButton)
        
        divideDetailImageView = UIImageView()
        divideDetailImageView.image = UIImage(named: "divideDetail")
        bgView.addSubview(divideDetailImageView)
        
        divideDetailLabel = HWLabel.createBoldLabel(title: "分成明细", color: "#333333", font: 14*widthScale)
        bgView.addSubview(divideDetailLabel)
        
        divideDetailButton = UIButton(type: .custom)
        divideDetailButton.addTarget(self, action: #selector(divideDetailButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(divideDetailButton)
        
        popularityImageView = UIImageView()
        popularityImageView.image = UIImage(named: "popularity")
        bgView.addSubview(popularityImageView)
        
        popularityLabel = HWLabel.createBoldLabel(title: "人气榜", color: "#333333", font: 14*widthScale)
        bgView.addSubview(popularityLabel)
        
        popularityButton = UIButton(type: .custom)
        popularityButton.addTarget(self, action: #selector(popularityButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(popularityButton)
        
        bottomView = UIView()
        bottomView.backgroundColor = .colorWithHexString(hexString: "#DEE4FF")
        bgView.addSubview(bottomView)
        
        noticeImageView = UIImageView()
        noticeImageView.image = UIImage(named: "hornPromote")
        bottomView.addSubview(noticeImageView)
        
        titleBanner = FSPagerView()
        titleBanner.dataSource = self
        titleBanner.delegate = self
        titleBanner.isInfinite = true
        titleBanner.register(HWNoticeCell.self, forCellWithReuseIdentifier: "HWNoticeCell")
        titleBanner.automaticSlidingInterval = 3
        titleBanner.removesInfiniteLoopForSingleItem = true
        titleBanner.scrollDirection = .vertical
        bottomView.addSubview(titleBanner)
        
    }
    
    private func initFrame() -> Void {
        
        searchBgView.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.right.equalTo(-20*widthScale)
            make.top.equalTo(10*widthScale)
            make.height.equalTo(36*widthScale)
        }
        
        searchLogoImage.snp.makeConstraints { make in
            make.left.equalTo(10*widthScale)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(16*widthScale)
        }
        
        searchLabel.snp.makeConstraints { make in
            make.left.equalTo(searchLogoImage.snp.right).offset(2*widthScale)
            make.centerY.equalToSuperview()
        }
        
        searchButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        bgView.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.right.equalTo(-20*widthScale)
            make.top.equalTo(searchBgView.snp.bottom).offset(10*widthScale)
            make.height.equalTo(126*widthScale)
        }
        
        promoteNoticeImageView.snp.makeConstraints { make in
            make.left.equalTo(25*widthScale)
            make.top.equalTo(20*widthScale)
            make.width.height.equalTo(34*widthScale)
        }
        
        promoteNoticeLabel.snp.makeConstraints { make in
            make.centerX.equalTo(promoteNoticeImageView)
            make.top.equalTo(promoteNoticeImageView.snp.bottom).offset(9*widthScale)
        }
        
        promoteNoticeButton.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(promoteNoticeLabel)
            make.top.equalTo(promoteNoticeImageView)
        }
        
        promoteQRCodeImageView.snp.makeConstraints { make in
            make.left.equalTo(promoteNoticeImageView.snp.right).offset(51*widthScale)
            make.centerY.equalTo(promoteNoticeImageView)
            make.width.height.equalTo(34*widthScale)
        }
        
        promoteQRCodeLabel.snp.makeConstraints { make in
            make.centerX.equalTo(promoteQRCodeImageView)
            make.top.equalTo(promoteQRCodeImageView.snp.bottom).offset(9*widthScale)
        }
        
        promoteQRCodeButton.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(promoteQRCodeLabel)
            make.top.equalTo(promoteQRCodeImageView)
        }
        
        divideDetailImageView.snp.makeConstraints { make in
            make.left.equalTo(promoteQRCodeImageView.snp.right).offset(49*widthScale)
            make.centerY.equalTo(promoteNoticeImageView)
            make.width.height.equalTo(34*widthScale)
        }
        
        divideDetailLabel.snp.makeConstraints { make in
            make.centerX.equalTo(divideDetailImageView)
            make.top.equalTo(divideDetailImageView.snp.bottom).offset(9*widthScale)
        }
        
        divideDetailButton.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(divideDetailLabel)
            make.top.equalTo(divideDetailImageView)
        }
        
        popularityImageView.snp.makeConstraints { make in
            make.left.equalTo(divideDetailImageView.snp.right).offset(50*widthScale)
            make.centerY.equalTo(promoteNoticeImageView)
            make.width.height.equalTo(34*widthScale)
        }
        
        popularityLabel.snp.makeConstraints { make in
            make.centerX.equalTo(popularityImageView)
            make.top.equalTo(popularityImageView.snp.bottom).offset(9*widthScale)
        }
        
        popularityButton.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(popularityLabel)
            make.top.equalTo(popularityImageView)
        }
        
        bottomView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(26*widthScale)
        }
        
        noticeImageView.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(10*widthScale)
        }
        
        titleBanner.snp.makeConstraints { make in
            make.left.equalTo(noticeImageView.snp.right).offset(4*widthScale)
            make.right.equalTo(-10*widthScale)
            make.height.equalTo(26*widthScale)
            make.centerY.equalToSuperview()
        }
    }
}

extension HWPromoteTopView {
    @objc func searchButtonClick(_:UIButton) -> Void {
        self.searchButtonClick?()
    }
    
    @objc func promoteNoticeButtonClick(_ button:UIButton) -> Void {
        self.promoteNoticeButtonClick?()
    }
    
    @objc func promoteQRCodeButtonClick(_ button:UIButton) -> Void {
        self.promoteQRCodeButtonClick?()
    }
    
    @objc func divideDetailButtonClick(_ button:UIButton) -> Void {
        self.divideDetailButtonClick?()
    }
    
    @objc func popularityButtonClick(_ button:UIButton) -> Void {
        self.popularityButtonClick?()
    }
}

extension HWPromoteTopView:FSPagerViewDelegate,FSPagerViewDataSource {
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return models?.count ?? 0
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "HWNoticeCell", at: index) as! HWNoticeCell
        cell.titleLabel.text = models?[index].title
        return cell
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        didClickBanner?(models?[index] ?? HWNoticeModel())
    }
    
}
